/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    IBM Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.stp.policy_builder.tcpmonitor.views;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class URIMapper {
	private Node doc;
	private HashMap map;
	public URIMapper(Node doc){
		this.doc = doc ;
		map = new HashMap<String, String>();
	}
	
	public Map getNamespacePrefixMap(){
		if(doc instanceof Document){
			Element root = ((Document) doc).getDocumentElement();
			populateMap(root);
			//printMap();
			System.out.println("A Document");
		}
		else{
			populateMap(doc);
			System.out.println("A node");
		}
		return map;
			
	}
	
	private void populateMap(Node child){
		evaluateURI(child);
		NodeList children = child.getChildNodes();
		if(children.getLength()>0)
			System.out.println("has Children");
		
		for(int i=0;i<children.getLength();i++){
			Node temp = children.item(i);
			populateMap(temp);
			
		}
		
	}
	
	private void evaluateURI(Node node){
		if(node!=null){
			String prefix,ns;
			prefix = node.getPrefix();
			ns = node.getNamespaceURI();
			Object temp = map.get(prefix);
			
			if(temp!=null && temp.equals(ns)){
				return;
			}
			else if(prefix!=null && ns!=null){
				map.put(prefix, ns);
			}
		}
	}
	protected void printMap(){
		Map m = this.map;
		Iterator it = m.keySet().iterator();
		if(it.hasNext()==false)
			System.out.println("Empty Map");
		
		while(it.hasNext()){
			Object key = it.next();
			System.out.println(key + ":" +m.get(key));
		}
	}
	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		try {
			builderFactory.setAttribute("http://apache.org/xml/features/allow-java-encodings", new Boolean(true));
			builderFactory.setAttribute("http://apache.org/xml/features/continue-after-fatal-error", new Boolean(true));
			builderFactory.setNamespaceAware(true);
		} catch (Exception e) {
			// ignore
		}
		DocumentBuilder parser = builderFactory.newDocumentBuilder();
		
		if(parser.isNamespaceAware()){
			System.out.println("namespace enabled");
		}
		InputSource source = new InputSource("/home/usw/my3.xml");
		Document document = parser.parse(source);
		URIMapper mapper = new URIMapper(document);
		Map m = mapper.getNamespacePrefixMap();
		Iterator it = m.keySet().iterator();
		
		while(it.hasNext()){
			Object key = it.next();
			System.out.println(key + ":" +m.get(key));
		}
		//System.out.println("Hi");
	}
	
}
